package com.hmall.search.mq;

import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {
    @Autowired
    ISearchService searchService;

    //上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name ="up.queue"),
            exchange = @Exchange(name = "itemExchange",type = ExchangeTypes.TOPIC),
            key = "itemExchange.up"
    ))
    public void listenerItemUp(Long id){
       searchService.addItemById(id);
    }

    //下架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name ="down.queue"),
            exchange = @Exchange(name = "itemExchange",type = ExchangeTypes.TOPIC),
            key = "itemExchange.down"
    ))
    public void listenerItemDown(Long id){
        searchService.deleteItemById(id);
    }
}
